import java.net.Socket;
import java.net.ServerSocket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;



public class XMLServer
{

    public void start() throws Exception
    {
        String xml = "<cross-domain-policy>";
        xml = xml + "<allow-access-from domain=\"*\" to-ports=\"*\" />";
        xml = xml + "</cross-domain-policy>";

        ServerSocket serverSocket = new ServerSocket(5000);
        while(true)
        {
            try {
                Socket socket = serverSocket.accept();
                System.out.println("connect successfully....");
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                char[] by = new char[22];
                br.read(by,0,22);
                String head = new String(by);
                System.out.println("message header:" + head + ":");
                if(head.equals("<policy-file-request/>")) {
                    pw.print(xml + "\0");
                    pw.flush();
                } else {
                    pw.print(head + "\0");
                    pw.flush();
                    /*
                    ServerThread thread = new ServerThread(socket);
                    thread.start();
                    */
                }
            } catch (Exception e) {
                System.out.println("server throw exception" + e );
            }
        }
    }

    public static void main(String[] args)
    {
        try {
            new XMLServer().start();
        } catch (Exception e) {
            System.out.println("socket exception:" + e);
        }
    }
}

